<template>
	<el-card shadow="hover" style="height:500px;margin-top: 10px;width: 100%;margin-left: 10px">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="100px"
			label-position="left">
			<el-form-item label="角色名称" prop="roleName">
				<el-input v-model="form.roleName" clearable></el-input>
			</el-form-item>
			<el-form-item label="角色编码" prop="roleCode">
				<el-input v-model="form.roleCode" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sort" controls-position="right" :min="1"
					style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="岗位" prop="positionNumber">
				<el-select v-model="form.positionNumber" clearable filterable placeholder="请选择岗位" :loading="postLoading"
					style="width: 100%;">
					<el-option v-for="item in postOptions" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="备注" prop="description">
				<el-input v-model="form.description" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
	</el-card>
	{{ visible }}
	<div style="width: 100%;height: 70px;position: absolute;bottom: 0;border-top: 0px solid #E8E8E8;padding-top: 10px">
		<el-button style="margin-left: 30px" @click="close()">取 消</el-button>
		<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
	</div>

</template>

<script>
export default {
	emits: ['success', 'closed'],
	props: {
		datas: {}
	},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			isSaveing: false,
			postLoading: false,
			postOptions: [],
			//表单数据
			form: {
				id: "",
				label: "",
				alias: "",
				sort: "",
				positionNumber: "",
				positionName: "",
				status: "",
				description: ""
			},
			//验证规则
			rules: {
				sort: [
					{ required: true, message: '请输入排序', trigger: 'change' }
				],
				label: [
					{ required: true, message: '请输入角色名称' }
				],
				alias: [
					{ required: true, message: '请输入角色别名' }
				]
				// positionNumber: [
				// 	{ required: true, message: '请选择岗位' }
				// ]

			}
		}
	},
	async mounted() {
		// Object.assign(this.form, this.datas)
		var res = await this.$API.emp.posnList.get({ data: {} })
		if (res && res.code == 0) {
			this.postOptions = res.data.map(item => {
				return { value: item, label: item };
			});
		}
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.form.positionName = this.postOptions.find((item) => item.value == this.form.positionNumber).label
					var res = await this.$API.basic.role.edit.post({ data: this.form });
					this.$emit('success', this.form, this.mode)
					if (res.code == 0) {
						this.$emit('success', this.form, this.mode)
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				}
			})
		},
		close() {
			this.$emit('closed')
		},
		//表单注入数据
		setData(data) {
			// this.form.id = data.id
			// this.form.role_name = data.role_name
			// this.form.role_code = data.role_code
			// this.form.sort = data.sort
			// this.form.enable = data.enable
			// this.form.description = data.description

			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			Object.assign(this.form, data)
		}
	}
}
</script>

<style></style>
